package com.lxq.smartsign.client;

import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import com.google.android.material.navigation.NavigationView;
import com.lxq.smartsign.client.util.ClientConfig;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.AppBarConfiguration;
import androidx.navigation.ui.NavigationUI;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

public class MainActivity extends AppCompatActivity {

    private AppBarConfiguration mAppBarConfiguration;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = findViewById(R.id.toolbar);
        requestPermissions();
        setSupportActionBar(toolbar);
        DrawerLayout drawer = findViewById(R.id.drawer_layout);
        NavigationView navigationView = findViewById(R.id.nav_view);
        View navigationHeadView=navigationView.getHeaderView(0);
        TextView nameView=navigationHeadView.findViewById(R.id.personnel_name);
        TextView passwordView=navigationHeadView.findViewById(R.id.personnel_password);
        nameView.setText("姓名："+ClientConfig.NAME);
        passwordView.setText("工号："+ClientConfig.USER_ID);
        // Passing each menu ID as a set of Ids because each
        // menu should be considered as top level destinations.
        mAppBarConfiguration = new AppBarConfiguration.Builder(
                R.id.nav_home, R.id.nav_gallery, R.id.nav_slideshow)
                .setOpenableLayout(drawer)
                .build();
        NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
        NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
        NavigationUI.setupWithNavController(navigationView, navController);
    }


    /*申请手机权限，在oncreate()方法中调用*/
    public void requestPermissions(){
        String [] permissions = new String[]{
                Manifest.permission.CAMERA,
                Manifest.permission.RECORD_AUDIO,
                Manifest.permission.VIBRATE,
                Manifest.permission.BLUETOOTH,
                Manifest.permission.BLUETOOTH_ADMIN,
                Manifest.permission.ACCESS_FINE_LOCATION,
                Manifest.permission.ACCESS_COARSE_LOCATION
        };
        //判断Android版本是否大于等于6.0
        if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.M){
            Log.e("requestPermission","申请权限");
            //申请权限，这时会弹出提示授权，如果已经授权，则不会弹出
            requestPermissions(permissions,1);
        }else{
            Log.e("requestPermission","已获得权限");
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode==1){
            if (grantResults[0]!= PackageManager.PERMISSION_GRANTED||
                    grantResults[3]!= PackageManager.PERMISSION_GRANTED){
                new AlertDialog.Builder(this)
                        .setMessage("您拒绝授权，将无法使用部分功能")
                        .setPositiveButton("重新授权", (dialog, which) -> requestPermissions())
                        .setNegativeButton("关闭",null)
                        .show();
            }
        }
    }

    @Override
    public boolean onSupportNavigateUp() {
        NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
        return NavigationUI.navigateUp(navController, mAppBarConfiguration)
                || super.onSupportNavigateUp();
    }
}